Calculate slow and fast exponential moving averages for AAPL stock using historical data, visualize results and calculate return and performance metrics. Our strategy purchases the stock as the fast exponential moving average crosses above the slow moving average. This system does not go short.
[1] "AAPL"
[1] "^GSPC"
[1] "DAL"
[1] "Our sample considers OHLC data from 2007-01-03 through 2021-10-01 for Apple, Delta and the S&P500"
Date Open High
Min. :2007-01-03 Min. : 2.84 Min. : 2.93
1st Qu.:2010-09-09 1st Qu.: 9.69 1st Qu.: 9.77
Median :2014-05-19 Median : 23.27 Median : 23.49
Mean :2014-05-18 Mean : 33.07 Mean : 33.41
3rd Qu.:2018-01-24 3rd Qu.: 41.90 3rd Qu.: 42.50
Max. :2021-10-01 Max. :156.98 Max. :157.26
Low Close
Min. : 2.79 Min. : 2.79
1st Qu.: 9.58 1st Qu.: 9.67
Median : 23.03 Median : 23.31
Mean : 32.72 Mean : 33.08
3rd Qu.: 41.46 3rd Qu.: 42.02
Max. :154.39 Max. :156.69
[1] 33.88096
summary(SPX[,1:5])
Date Open High
Min. :2007-01-03 Min. : 679.3 Min. : 695.3
1st Qu.:2010-09-09 1st Qu.:1332.4 1st Qu.:1341.2
Median :2014-05-19 Median :1878.1 Median :1888.4
Mean :2014-05-18 Mean :2007.5 Mean :2018.5
3rd Qu.:2018-01-24 3rd Qu.:2605.1 3rd Qu.:2635.0
Max. :2021-10-01 Max. :4535.4 Max. :4545.9
Low Close
Min. : 666.8 Min. : 676.5
1st Qu.:1325.7 1st Qu.:1332.7
Median :1870.7 Median :1878.4
Mean :1995.5 Mean :2007.8
3rd Qu.:2585.5 3rd Qu.:2609.0
Max. :4524.7 Max. :4536.9
sd(SPX$Close)
[1] 850.4188
summary(dal_df[,1:5])
Date Open High Low
Min. :2007-05-03 Min. : 3.90 Min. : 4.06 Min. : 3.51
1st Qu.:2010-12-07 1st Qu.:11.27 1st Qu.:11.55 1st Qu.:10.98
Median :2014-07-18 Median :30.91 Median :31.51 Median :30.30
Mean :2014-07-17 Mean :30.24 Mean :30.69 Mean :29.76
3rd Qu.:2018-02-24 3rd Qu.:47.53 3rd Qu.:48.17 3rd Qu.:46.86
Max. :2021-10-01 Max. :63.23 Max. :63.44 Max. :62.38
Close
Min. : 3.93
1st Qu.:11.29
Median :30.90
Mean :30.22
3rd Qu.:47.52
Max. :63.16
sd(dal_df$Close)
[1] 18.25302
Calculate a simple day-to-day return, from adjusted-close to adjusted-close and store in return and log_return vector:
Date Close return log_return
1 2007-01-03 2.992857 NA NA
2 2007-01-04 3.059286 0.022195838 0.021953096
3 2007-01-05 3.037500 -0.007121340 -0.007146817
4 2007-01-08 3.052500 0.004938561 0.004926407
5 2007-01-09 3.306071 0.083069464 0.079799106
6 2007-01-10 3.464286 0.047856267 0.046746426
Date Close return log_return
1 2007-05-03 20.72 NA NA
2 2007-05-04 20.98 0.012548146 0.012470070
3 2007-05-07 20.36 -0.029551530 -0.029996974
4 2007-05-08 19.63 -0.035855042 -0.036513624
5 2007-05-09 19.79 0.008151280 0.008118237
6 2007-05-10 19.70 -0.004548079 -0.004558453
Date Close return log_return
1 2007-01-03 1416.60 NA NA
2 2007-01-04 1418.34 0.0012282861 0.0012275323
3 2007-01-05 1409.71 -0.0060845814 -0.0061031679
4 2007-01-08 1412.84 0.0022203184 0.0022178572
5 2007-01-09 1412.11 -0.0005166764 -0.0005168099
6 2007-01-10 1414.85 0.0019403524 0.0019384723
Create a function to calculate two simple moving averages of differing lengths for use for a trend-following trading system (for this system we won’t be using a simple moving average):
Create a function to calculate two exponential moving averages of differing lengths for use for a trend-following trading system and store in fast_ma, slow_ma and ema_diff vector:
Date Open High Low Close Volume Price
3709 2021-09-24 145.66 147.47 145.56 146.92 53477900 146.92
3710 2021-09-27 145.47 145.96 143.82 145.37 74150700 145.37
3711 2021-09-28 143.25 144.75 141.69 141.91 108972300 141.91
3712 2021-09-29 142.47 144.45 142.03 142.83 74602000 142.83
3713 2021-09-30 143.66 144.38 141.28 141.50 88934200 141.50
3714 2021-10-01 141.90 142.92 139.12 142.65 89362470 142.65
return log_return slow_ma fast_ma ema_diff
3709 0.0006129265 0.0006127387 137.7536 148.2027 -10.449156
3710 -0.0105499797 -0.0106060253 137.8545 147.9848 -10.130373
3711 -0.0238012734 -0.0240891000 137.9082 147.5175 -9.609362
3712 0.0064829679 0.0064620438 137.9734 147.1570 -9.183591
3713 -0.0093117831 -0.0093554088 138.0201 146.7218 -8.701730
3714 0.0081271661 0.0080943185 138.0814 146.4086 -8.327189
Date Open High Low Close Volume Price return
3626 2021-09-24 42.84 43.74 42.67 43.53 13478500 43.53 0.022070885
3627 2021-09-27 43.90 45.06 43.75 43.78 14467500 43.78 0.005743166
3628 2021-09-28 43.66 44.21 43.31 43.44 9434700 43.44 -0.007766103
3629 2021-09-29 43.69 43.99 42.75 43.36 9141100 43.36 -0.001841575
3630 2021-09-30 43.06 43.08 42.37 42.61 9789200 42.61 -0.017297048
3631 2021-10-01 43.95 45.65 43.89 45.38 19185900 45.38 0.065008213
log_return slow_ma fast_ma ema_diff
3626 0.021830849 42.00397 40.73510 1.2688679
3627 0.005726737 42.02749 40.96932 1.0581685
3628 -0.007796417 42.04620 41.15937 0.8868252
3629 -0.001843272 42.06360 41.32865 0.7349476
3630 -0.017448389 42.07084 41.42722 0.6436195
3631 0.062982510 42.11467 41.73128 0.3833893
First calculate the maximum of the aforementioned ranges and store in max_range vector:
Date Open High Low Close max_range
25 2007-02-07 3.017143 3.085000 2.983929 3.076786 0.101071
26 2007-02-08 3.051071 3.089643 3.050357 3.077857 0.039286
27 2007-02-09 3.067143 3.078571 2.971786 2.973929 0.106785
28 2007-02-12 3.015357 3.042143 2.986786 3.031429 0.068214
29 2007-02-13 3.041429 3.046071 3.010714 3.025000 0.035357
30 2007-02-14 3.022500 3.058571 3.020357 3.046429 0.038214
31 2007-02-15 3.051429 3.057857 3.027857 3.043214 0.030000
32 2007-02-16 3.044643 3.050357 3.023571 3.029643 0.026786
Date Open High Low Close max_range
25 2007-06-07 18.77 18.85 18.15 18.48 0.780000
26 2007-06-08 18.20 18.83 18.20 18.83 0.629999
27 2007-06-11 18.55 18.82 18.22 18.38 0.610001
28 2007-06-12 18.15 18.71 18.02 18.40 0.689999
29 2007-06-13 18.48 18.70 18.36 18.64 0.340000
30 2007-06-14 18.50 19.01 18.43 18.90 0.580000
31 2007-06-15 18.89 19.14 18.45 18.88 0.689998
32 2007-06-18 18.65 18.90 18.50 18.54 0.400000
Create function that uses the true range vector to calculate a moving average of the true range with a user-specified lag period and store in atr vector:
This is a multiple of the ATR for volatility-based position sizing:
Store in risk_per_lot vector:
Close max_range atr risk_per_lot
200 6.169643 0.137857 0.1771204 0.8856022
201 6.196429 0.112142 0.1757588 0.8787941
202 6.086429 0.165357 0.1811829 0.9059147
203 6.227143 0.176429 0.1880803 0.9404013
204 6.648571 0.508571 0.2098659 1.0493297
205 6.640357 0.284642 0.2216292 1.1081462
206 6.527857 0.152500 0.2263167 1.1315837
207 6.596429 0.092500 0.2218748 1.1093741
208 6.610357 0.067500 0.2141739 1.0708697
209 6.678571 0.165714 0.2101114 1.0505569
210 6.783929 0.184643 0.2117632 1.0588159
Close max_range atr risk_per_lot
200 17.32 0.529998 1.0568747 5.284374
201 16.77 1.139999 1.0693747 5.346873
202 16.81 0.920000 1.0781246 5.390623
203 16.21 1.340000 1.1168747 5.584373
204 16.37 0.900000 1.1268747 5.634373
205 16.15 1.140001 1.1293748 5.646874
206 15.91 0.709999 1.0362498 5.181249
207 15.00 1.160000 1.0031248 5.015624
208 14.09 1.320000 1.0262498 5.131249
209 13.35 1.080000 0.9649998 4.824999
210 12.98 0.850000 0.9524999 4.762499
Date Open High Low Close Volume
274 2008-02-04 4.793214 4.853571 4.693571 4.701786 899234000
428 2008-09-12 5.389643 5.389643 5.232143 5.319286 793027200
1477 2012-11-12 19.791071 19.803572 19.237499 19.386786 515802000
2166 2015-08-10 29.132500 29.997499 29.132500 29.930000 219806400
2340 2016-04-19 26.969999 27.000000 26.557501 26.727501 129539600
2995 2018-11-21 44.932499 45.067501 44.137501 44.195000 124496800
3126 2019-06-04 43.860001 44.957500 43.630001 44.910000 123872000
3332 2020-03-27 63.187500 63.967499 61.762501 61.935001 204216800
Price return log_return slow_ma fast_ma
274 4.037031 -0.015700877 -0.015825441 5.55234 5.506563
428 4.567226 -0.024304031 -0.024604248 5.87707 5.846986
1477 16.793638 -0.007732104 -0.007762152 21.45779 21.451680
2166 27.481977 0.036357387 0.035712053 30.52147 30.497427
2340 24.780203 -0.005303080 -0.005317191 26.90771 26.907096
2995 42.966122 -0.001130171 -0.001130810 50.35792 50.248957
3126 44.017365 0.036584266 0.035930948 46.67191 46.601018
3332 61.262600 -0.041402369 -0.042283863 66.46632 66.384702
ema_diff max_range atr risk_per_lot
274 0.0457772571 0.160000 0.3344641 1.672321
428 0.0300839936 0.219643 0.2106028 1.053014
1477 0.0061117923 0.566073 0.6715623 3.357812
2166 0.0240385749 1.117500 0.7928124 3.964062
2340 0.0006101917 0.442499 0.5092187 2.546094
2995 0.1089631055 0.930000 1.8137499 9.068749
3126 0.0708910235 1.632499 1.3182809 6.591404
3332 0.0816144640 2.847500 4.9378125 24.689063
Date Open High Low Close Volume
326 2008-04-18 5.682857 5.795000 5.656429 5.751429 1026765600
574 2009-04-14 4.270357 4.291786 4.187500 4.225357 454622000
1668 2013-08-16 17.862499 17.962143 17.816429 17.940357 362306000
2338 2016-04-15 28.027500 28.075001 27.432501 27.462500 187756000
2417 2016-08-08 26.879999 27.092501 26.790001 27.092501 112148800
3078 2019-03-26 47.915001 48.220001 46.145000 46.697498 199202000
3130 2019-06-10 47.952499 48.842499 47.904999 48.145000 104883600
3345 2020-04-16 71.845001 72.050003 70.587502 71.672501 157125200
Price return log_return slow_ma fast_ma
326 4.938271 0.042397806 0.041523642 5.214082 5.228581
574 3.627961 -0.015887031 -0.016014582 3.847673 3.868581
1668 15.838611 0.008876985 0.008837816 16.415864 16.450733
2338 25.461653 -0.020071263 -0.020275427 26.910664 26.926401
2417 25.408445 0.008280830 0.008246732 25.439263 25.440248
3078 45.593746 -0.010331709 -0.010385452 45.161955 45.256133
3130 47.188053 0.012779295 0.012698329 46.684806 46.708433
3345 70.894394 0.007945857 0.007914455 66.394240 66.711820
ema_diff max_range atr risk_per_lot
326 -0.0144994403 0.277500 0.1823213 0.9116066
574 -0.0209079076 0.106071 0.1438391 0.7191953
1668 -0.0348688448 0.179644 0.3495761 1.7478803
2338 -0.0157370471 0.642500 0.4750002 2.3750012
2417 -0.0009848558 0.302500 0.4473437 2.2367184
3078 -0.0941787793 2.075001 1.0417192 5.2085959
3130 -0.0236271939 1.305001 1.2028128 6.0140638
3345 -0.3175799811 1.462501 2.6524994 13.2624972
Date Open High Low Close Volume Price
61 2007-07-30 18.50 18.59 17.32 18.05 8926900 16.032856
134 2007-11-09 16.50 17.14 16.40 16.50 3750000 14.656072
389 2008-11-13 7.26 8.17 7.19 8.17 15547200 7.256976
444 2009-02-04 7.00 7.12 6.50 6.51 11712200 5.782486
810 2010-07-20 11.18 11.62 11.10 11.53 9408900 10.241486
939 2011-01-21 11.71 11.78 11.39 11.54 15559900 10.250369
1324 2012-08-01 9.49 9.62 9.40 9.48 15043400 8.420581
1876 2014-10-13 33.30 33.31 30.12 30.90 41248000 27.754200
2039 2015-06-08 42.37 42.62 40.65 40.75 18359700 36.828819
2201 2016-01-28 44.84 45.00 42.52 43.20 13996900 39.261044
2260 2016-04-22 43.43 44.67 43.16 44.62 14258600 40.680176
2504 2017-04-11 45.00 45.32 44.49 45.29 10506800 42.010307
2599 2017-08-25 45.43 47.06 45.35 46.68 13938800 43.758713
2775 2018-05-09 52.17 52.28 51.46 51.62 8403900 48.971581
2809 2018-06-27 51.03 51.18 49.70 49.89 9035800 47.611656
2888 2018-10-18 54.29 54.30 52.96 53.13 5871200 51.049423
2932 2018-12-21 50.51 51.28 49.29 49.45 11680200 47.812092
3132 2019-10-09 54.01 54.40 53.59 53.92 7615300 53.155052
3172 2019-12-05 56.39 56.41 55.70 55.88 3899900 55.496494
3227 2020-02-26 51.44 51.77 49.00 49.59 15985900 49.590000
3577 2021-07-16 41.83 42.05 39.84 40.06 16726600 40.060001
return log_return slow_ma fast_ma ema_diff
61 -0.029047622 -0.029477856 20.166701 20.108971 0.057729287
134 -0.029411611 -0.029852805 19.150417 19.031495 0.118921509
389 0.108548100 0.103051141 8.912571 8.869034 0.043537213
444 -0.056521860 -0.058182084 9.366152 9.281247 0.084904999
810 0.013180917 0.013094804 12.138533 12.121677 0.016855875
939 -0.006029258 -0.006047507 12.441762 12.429321 0.012441020
1324 -0.017616583 -0.017773601 10.136524 10.119460 0.017064219
1876 -0.061075525 -0.063020234 36.256417 36.147118 0.109298451
2039 -0.050116462 -0.051415893 43.967611 43.864808 0.102802772
2201 -0.033988931 -0.034579987 47.192733 46.987755 0.204978588
2260 -0.007782943 -0.007813388 47.098074 47.017579 0.080495096
2504 0.007115805 0.007090607 46.402673 46.363001 0.039671337
2599 0.032515034 0.031997607 49.272102 49.117258 0.154844312
2775 -0.014321108 -0.014424645 53.213014 53.148667 0.064346118
2809 -0.023105571 -0.023376689 53.365226 53.318601 0.046625036
2888 -0.021546911 -0.021782435 54.346382 54.271469 0.074913573
2932 -0.028677958 -0.029097205 54.838522 54.693107 0.145415314
3132 0.013533824 0.013443060 56.682551 56.506699 0.175852095
3172 -0.003210852 -0.003216018 56.330176 56.322689 0.007486824
3227 -0.025545275 -0.025877220 57.180516 56.932703 0.247812409
3577 -0.031197027 -0.031694018 43.547324 43.329234 0.218090262
max_range atr risk_per_lot
61 1.270000 0.7862494 3.931247
134 0.739999 0.9568752 4.784376
389 0.980000 1.2818750 6.409375
444 0.620000 1.1000000 5.500000
810 0.520000 0.6918750 3.459375
939 0.390000 0.4356250 2.178125
1324 0.250000 0.4187500 2.093750
1876 3.190000 1.4331254 7.165627
2039 2.250000 1.4418755 7.209378
2201 2.480000 2.0868748 10.434374
2260 1.810001 1.3693744 6.846872
2504 0.829998 0.9218751 4.609375
2599 1.850002 1.0218759 5.109379
2775 0.910000 1.2231251 6.115625
2809 1.479999 1.2256246 6.128123
2888 1.340000 1.4993750 7.496875
2932 1.989998 1.8862503 9.431251
3132 1.200001 1.3774996 6.887498
3172 0.709999 0.9981257 4.990628
3227 2.770000 1.7006252 8.503126
3577 2.209999 1.3481249 6.740625
Date Open High Low Close Volume Price
53 2007-07-18 21.79 21.80 21.15 21.37 9011300 18.981836
121 2007-10-23 20.18 21.00 20.10 20.82 5704400 18.493296
383 2008-11-05 11.15 11.36 10.28 10.48 17223200 9.308827
406 2008-12-09 10.78 11.64 10.53 10.87 18926300 9.655241
586 2009-08-27 7.48 7.50 7.25 7.49 10430300 6.652969
876 2010-10-21 13.03 13.54 12.90 13.53 20095600 12.017979
1190 2012-01-20 9.30 9.50 9.25 9.41 7844600 8.358402
1381 2012-10-22 10.00 10.20 9.90 10.14 7317200 9.006823
1886 2014-10-27 39.38 40.10 39.20 39.75 18958800 35.703213
2075 2015-07-29 44.81 45.33 43.85 44.18 10355300 39.928761
2225 2016-03-03 48.59 49.01 48.35 48.79 8853700 44.481972
2396 2016-11-03 42.06 42.57 41.77 41.92 8142800 38.552368
2520 2017-05-04 48.46 48.80 48.20 48.64 7467400 45.117710
2628 2017-10-06 51.40 52.52 51.40 52.01 6735400 48.755154
2786 2018-05-24 53.53 54.57 53.53 54.40 5251200 51.915695
2837 2018-08-07 54.60 55.09 54.52 54.70 4425300 52.557941
2900 2018-11-05 56.02 56.38 55.55 56.05 5206000 53.855076
3003 2019-04-05 57.44 58.20 57.33 57.73 6918300 56.207935
3167 2019-11-27 57.53 57.58 56.68 57.07 4062500 56.678326
3179 2019-12-16 57.17 58.49 57.15 58.42 7460700 58.019058
3409 2020-11-12 34.09 35.55 34.02 34.38 21676300 34.380001
return log_return slow_ma fast_ma ema_diff
53 0.0084946029 0.0084587268 20.159647 20.169596 -0.009948802
121 0.0409998663 0.0401816612 19.134454 19.222614 -0.088160161
383 -0.0709218485 -0.0735624194 8.929550 9.012640 -0.083090275
406 -0.0136120187 -0.0137055116 8.833022 8.871540 -0.038518040
586 0.0040215377 0.0040134730 6.907947 6.930079 -0.022132016
876 0.0431764527 0.0422703397 11.713555 11.730301 -0.016745590
1190 0.0085744007 0.0085378494 8.547009 8.562098 -0.015088856
1381 0.0140000504 0.0139029549 9.778188 9.798855 -0.020667547
1886 0.0078600078 0.0078292789 36.246599 36.426752 -0.180152899
2075 -0.0162547209 -0.0163882782 43.440206 43.444856 -0.004649717
2225 0.0049434111 0.0049312325 46.839241 46.900670 -0.061428707
2396 -0.0002383202 -0.0002383486 40.537933 40.600753 -0.062820484
2520 0.0068307966 0.0068075724 46.334027 46.377038 -0.043010716
2628 0.0063854028 0.0063651025 49.020699 49.140316 -0.119616788
2786 0.0187265392 0.0185533563 53.202877 53.215520 -0.012642452
2837 0.0051452410 0.0051320494 52.954865 52.959755 -0.004889834
2900 -0.0012473680 -0.0012481467 54.339901 54.442930 -0.103028901
3003 0.0089128456 0.0088733606 52.148067 52.183860 -0.035793512
3167 -0.0067872883 -0.0068104267 56.331252 56.349205 -0.017952846
3179 0.0297901729 0.0293550658 56.332870 56.381922 -0.049052231
3409 -0.0182752427 -0.0184442978 32.438902 32.526345 -0.087443498
max_range atr risk_per_lot
53 0.649999 0.5424997 2.712499
121 1.000000 0.7812501 3.906251
383 1.080000 1.3375000 6.687500
406 1.110000 1.0050000 5.025000
586 0.250000 0.3500000 1.750000
876 0.640000 0.4850000 2.425000
1190 0.250000 0.3181250 1.590625
1381 0.300000 0.3275000 1.637500
1886 0.899997 1.8275003 9.137502
2075 1.480004 1.2612499 6.306249
2225 0.660000 1.2812494 6.406247
2396 0.800000 1.1793752 5.896876
2520 0.599998 1.0999990 5.499995
2628 1.119998 1.0718753 5.359377
2786 1.169998 1.0900004 5.450002
2837 0.670002 1.1387506 5.693753
2900 0.830002 1.6181255 8.090627
3003 0.980000 1.2262497 6.131249
3167 0.900002 0.8443754 4.221877
3179 1.760002 1.0968751 5.484375
3409 1.529999 1.9068745 9.534372
The following candlestick chart helps us to visualize what our strategy is doing
As the faster price smoothing crosses above the slower lag, the system will buy on that day.
Now we use our indicator to generate trading signals and store them in signal vector:
Date Close slow_ma fast_ma signal
200 2007-10-17 6.169643 4.564776 5.606869 H
201 2007-10-18 6.196429 4.586387 5.652220 H
202 2007-10-19 6.086429 4.606255 5.685621 H
203 2007-10-22 6.227143 4.627724 5.727276 H
204 2007-10-23 6.648571 4.654490 5.798145 H
205 2007-10-24 6.640357 4.680793 5.862931 H
206 2007-10-25 6.527857 4.705257 5.914079 H
207 2007-10-26 6.596429 4.730306 5.966567 H
208 2007-10-29 6.610357 4.755207 6.016090 H
209 2007-10-30 6.678571 4.780682 6.067050 H
210 2007-10-31 6.783929 4.807215 6.122194 H
Date Close slow_ma fast_ma signal
200 2008-02-15 17.32 17.58714 16.83938 H
201 2008-02-19 16.77 17.57632 16.83404 H
202 2008-02-20 16.81 17.56617 16.83219 H
203 2008-02-21 16.21 17.54820 16.78433 H
204 2008-02-22 16.37 17.53260 16.75246 H
205 2008-02-25 16.15 17.51429 16.70612 H
206 2008-02-26 15.91 17.49304 16.64488 H
207 2008-02-27 15.00 17.46002 16.51835 H
208 2008-02-28 14.09 17.41538 16.33155 H
209 2008-02-29 13.35 17.36154 16.10220 H
210 2008-03-03 12.98 17.30350 15.86203 H
After generating the signals, we must calculate the return from our strategy:
Store in investment_return vector
From the individual daily returns, we must create a cash and number of stocks vector to hold each days current figures.
Calculate and store in cash and n_stock vector:
Calculate the cash plus the value of the stock on hand for the last day in the sample.
Print final return from trend following system and buy-and-hold strategy:
[1] "Initial Investment: $ 100000.00"
[1] "Trend-following system Final Return: $ 2454037.68"
[1] "Buy and hold system Final Return: $ 4528709.21"
[1] "Initial Investment: $ 100000.00"
[1] "Trend-following system Final Return: $ 71072.99"
[1] "Buy and hold system Final Return: $ 108627.11"
Date Open High Low Close Volume Price
3323 2020-03-16 60.4875 64.77 60 60.5525 322423600 59.89511
return log_return slow_ma fast_ma ema_diff max_range
3323 -0.128647 -0.137708 67.15978 71.94174 -4.781956 9.4925
atr risk_per_lot MACD_direction signal holding Inv_return
3323 5.212187 26.06094 Decreasing H 1 -0.1286469
cash n_stock
3323 45.31682 19623
Date Open High Low Close Volume Price
3322 2020-03-13 66.2225 69.98 63.2375 69.4925 370732000 68.73805
return log_return slow_ma fast_ma ema_diff max_range
3322 0.1198084 0.1131576 67.24847 72.89084 -5.64237 7.922504
atr risk_per_lot MACD_direction signal holding Inv_return
3322 4.774375 23.87188 Decreasing H 1 0.1198083
cash n_stock
3322 45.31682 19623
Retrieve the days with the largest single-day loss, gain and average daily return for a simple buy and hold system, as well as for our trend-following system.
Compare the results from the two strategies.
[1] "Largest 1-day return with trend system: 11.9808 %"
[1] "Largest 1-day loss with trend system: -12.8647 %"
[1] "Average 1-day with trend system return: 0.1006 %"
[1] "Largest 1-day return buy and hold strategy: 13.9049 %"
[1] "Largest 1-day loss with buy and hold strategy: -17.9196 %"
[1] "Average 1-day with buy and hold strategy: 0.1289 %"
Create a histogram of returns for a simple buy and hold strategy, as well as for our trend-following system:
Retrieve the average daily return and volatility of our trend-following system, as well as a simple buy and hold strategy:
[1] "Average return: 0.001006"
[1] "Volatility: 0.01507"
[1] "Average return buy hold system: 0.001289"
[1] "Volatility buy hold system: 0.020298"
[1] "Buy and hold strategy experiences larger volatility for a small amount of additional return"
[1] "DAL Average return: 0.000117"
[1] "DAL Volatility: 0.018258"
[1] "As we can see, the average return when using this system for DAL is much smaller and the volatility is larger. We can expect the Sharpe ratio to be much smaller in this environment"
The Sharpe ratio was developed by Nobel laureate William F. Sharpe and is used to help investors understand the return of an investment compared to its risk.12 The ratio is the average return earned in excess of the risk-free rate per unit of volatility or total risk. Volatility is a measure of the price fluctuations of an asset or portfolio.
[1] "AAPL Trend following: Sharpe ratio of daily returns: 0.066"
[1] "AAPL Buy & hold: Sharpe ratio of daily returns: 0.063"
[1] "Our AAPL Trend-following system achieves a slightly higher Sharpe ratio"
[1] "DAL Trend following: Sharpe ratio of daily returns: 0.00586"
[1] "Compared to Apple Trend-following returns, DAL's example provides a much smaller Sharpe ratio - this is due to whipsaw-like nature of Delta's historical stock price and the trend-following's lack of ability to trade in this type of environment (at least with the parameters we have chosen)"
VAR measures the cut-off return that your financial asset will fall below with certain probability.
Value at risk (VaR)Â is a statistic that quantifies the extent of possible financial losses within a firm, portfolio, or position over a specific time frame. This metric is most commonly used by investmentand commercial banks to determine the extent and probabilities of potential losses in their institutional portfolios.
[1] "Apple Trend-following VAR: we can expect 95% of the time returns will be greater than: -2.3186"
[1] "Apple Buy / Hold model VAR: we can expect 95% of the time returns will be greater than: -2.9607"
[1] "DAL Trend-following VAR: we can expect 95% of the time returns will be greater than: -2.8592"
In order to break down risk into two components, we need to collect what part of the return was attributed to the market and what part to our trading system.
Our \(y\) is the excess return of the trading system
Our \(x\) is the excess return of the market
Call:
lm(formula = y ~ x)
Residuals:
Min 1Q Median 3Q Max
-0.084203 -0.006313 -0.000700 0.006411 0.099545
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.0007870 0.0002186 3.6 0.000322 ***
x 0.5673610 0.0167862 33.8 < 2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 0.01314 on 3613 degrees of freedom
(99 observations deleted due to missingness)
Multiple R-squared: 0.2402, Adjusted R-squared: 0.24
F-statistic: 1142 on 1 and 3613 DF, p-value: < 2.2e-16
[1] "Trend-following system Beta: 0.5674"
The larger the \(BETA\) is, the larger the systematic risk is.
We use this to measure the aggressiveness of the stock or the trading system.
Our trend-following system is mostly conservative.
The regression model’s intercept term tells us how much the strategy gives us extra reward, above the market return.
Generally we would like to see a positive \(ALPHA\)
This is associated with its statistical significance.
The higher \(ALPHA\) the better the investment performance
[1] "Jensen's Alpha: 8e-04"
[1] "Treynor's Ratio Version 1: 0.0014"
[1] "Treynor's Ratio Version 2: 0.0018"
raw_data$portfolio_value = raw_data$n_stock*raw_data$Close + raw_data$cash
# Plot main data
fig <- raw_data %>% plot_ly(x = ~Date, type="ohlc",
open = ~Open, close = ~Close,
high = ~High, low = ~Low, name = "AAPL",
increasing = i, decreasing = d)
# Add Fast and Slow moving average lines
fig <- fig %>% add_lines(x = ~Date, y = ~slow_ma, name = "Slow EMA",
line = list(color = '#ccc', width = 0.75),
legendgroup = "Bands", inherit = F,
showlegend = TRUE, hoverinfo = "none")
fig <- fig %>% add_lines(x = ~Date, y = ~fast_ma, name = "Fast EMA",
line = list(color = '#E377C2', width = 0.75),
legendgroup = "bands",
hoverinfo = "none", inherit = F)
ay <- list(
tickfont = list(color = "grey"),
overlaying = "y",
side = "right",
title = "<b>Secondary:</b> Portfolio Value")
fig <- fig %>% add_lines(x = ~Date, y = ~portfolio_value, name = "Portfolio Value",
line = list(color = 'limegreen', width = 1), dash = 'dot', yaxis = "y2")
# Add y-axis title
fig <- fig %>% layout(yaxis = list(title = "Price"))
# Add arrow annotations
fig <- fig %>% layout(annotations = h_a)
fig <- fig %>% layout(annotations = l_a)
# Add title
fig <- fig %>% layout(
title = "<b>Trend-Following System:</b> Portfolio Value", yaxis2 = ay,
xaxis = list(rangeslider = list(visible = F)),
yaxis = list(title = "<b>Primary:</b> Stock Price")
) %>%
layout(plot_bgcolor = 'rgb(255, 255, 255)',
xaxis = list(
zerolinecolor = '#ffffff',
zerolinewidth = 2,
gridcolor = '#ffffff'),
yaxis = list(
zerolinecolor = '#ffffff',
zerolinewidth = 2,
gridcolor = '#ffffff')
)
fig